﻿using System;
using System.Runtime.Remoting.Contexts;
using System.Text;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using RabbitMQ.Client.Framing;

namespace MQ1
{
    class Program
    {
        static ConnectionFactory connectionFactory1 = new ConnectionFactory { HostName = "localhost" };
        static ConnectionFactory connectionFactory2 = new ConnectionFactory { HostName = "localhost" };

        static void Main(string[] args)
        {
            //SendMessage("Eric_Exchanges_001", "Eric_Queues_001", "Test123456");

           // Consume("Eric_Queues_001"); // 一对一模式

            Consume("Eric_Queues_fanout_001"); // 广播模式
            Consume2("Eric_Queues_fanout_002"); // 广播模式

            Console.Read();
            //ReceiveMessage("Eric_Queues_001");
        }

        /// <summary>
        /// 发送一条消息
        /// </summary>
        /// <param name="exchange"></param>
        /// <param name="routeKey"></param>
        /// <param name="message"></param>
        public static void SendMessage(string exchange, string routeKey, string message)
        {
            var connectionFactory = new ConnectionFactory { HostName = "localhost" };
            using (var connection = connectionFactory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    BasicProperties basicProperties = new BasicProperties { Persistent = true };
                    var payload = Encoding.UTF8.GetBytes(message);

                    channel.BasicPublish(exchange, routeKey, basicProperties, payload);
                }
            }
        }

        /// <summary>
        /// 持续接收新消息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queue"></param>
        /// <returns></returns>
        public static void Consume(string queue)
        {

            var connection = connectionFactory1.CreateConnection();
            var channel = connection.CreateModel();

            //var consumer = new EventingBasicConsumer(channel);

            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += (ch, deliver) =>
            {
                string jsonString = Encoding.UTF8.GetString(deliver.Body);
                Console.WriteLine(string.Format("消费者1：{0}",jsonString));
            };
            channel.BasicConsume(queue, true, consumer);


        }

        /// <summary>
        /// 持续接收新消息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="queue"></param>
        /// <returns></returns>
        public static void Consume2(string queue)
        {
            var connection = connectionFactory2.CreateConnection();
            var channel = connection.CreateModel();

            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += (ch, deliver) =>
            {
                string jsonString = Encoding.UTF8.GetString(deliver.Body);
                Console.WriteLine(string.Format("消费者2：{0}", jsonString));
            };
            channel.BasicConsume(queue, true, consumer);

        }

        /// <summary>
        /// 接收一条消息
        /// </summary>
        /// <param name="queue"></param>
        /// <returns></returns>
        public static void ReceiveMessage(string queue)
        {
            var connectionFactory = new ConnectionFactory { HostName = "localhost" };
            using (var connection = connectionFactory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    var consumer = new QueueingBasicConsumer(channel);
                    channel.BasicConsume(queue, true, consumer);

                    var deliver = consumer.Queue.Dequeue();

                    string jsonString = Encoding.UTF8.GetString(deliver.Body);

                    Console.WriteLine(jsonString);
                }
            }
            Console.Read();
        }
    }
}
